package com.SmartCare.service.impl.role;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.SmartCare.common.enums.Constants;
import com.SmartCare.common.Result;
import com.SmartCare.common.enums.ElderlyCareRole;
import com.SmartCare.common.exception.ServiceException;
import com.SmartCare.common.vo.role.ElderVo;
import com.SmartCare.mapper.role.ElderlyInformationMapper;
import com.SmartCare.mapper.role.EmployeeElderMapper;
import com.SmartCare.mapper.role.UserMapper;
import com.SmartCare.pojo.role.ElderlyInformation;
import com.SmartCare.pojo.role.EmployeeElder;
import com.SmartCare.pojo.role.User;
import com.SmartCare.service.role.EmployeeElderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 职工老人关系表 服务实现类
 */
@Service
public class EmployeeElderServiceImpl extends ServiceImpl<EmployeeElderMapper, EmployeeElder> implements EmployeeElderService {

    @Resource
    ElderlyInformationMapper eim;

    @Resource
    UserMapper userMapper;

    @Override
    public Result getElderList(Long workerId) {
        //查询数据库
        //检测护工是否存在
        User user = userMapper.selectById(workerId);
        if (user == null) {
            throw new ServiceException(Constants.CODE_400, "护工不存在");
        }
        else if (user.getRole() != ElderlyCareRole.CAREGIVER.getValue()) {
            throw new ServiceException(Constants.CODE_400, "该用户不是护工");
        }
        else if (!user.getStatus()) {
            throw new ServiceException(Constants.CODE_400,"用户已禁用");
        }


        //根据护工id先获取中间表数据
        QueryWrapper<EmployeeElder> qw = new QueryWrapper<>();
        qw.eq("employee_id", workerId);
        List<EmployeeElder> list = list(qw);
        //根据中间表数据获取老人id数组
        List<Long> ids = new ArrayList<>();
        for (EmployeeElder e : list) {
            ids.add(e.getElderlyId());
        }
        //根据老人ids获取老人信息列表
        if (ids.size() == 0) {
            throw new ServiceException(Constants.CODE_400, "没有该护工负责的老人信息");
        }
        List<ElderlyInformation> elders = eim.selectBatchIds(ids);
        //逐个将老人信息复制到vo上返回
        List<ElderVo> result = new ArrayList<>();
        for (ElderlyInformation elder : elders) {
            ElderVo ev = new ElderVo();
            BeanUtil.copyProperties(elder, ev, true);
            //修改需要判断的两个字段
            ev.setSex(ev.getSex().equals("1") ?"男":"女");
            ev.setSelfCareAbility(elder.getSelfCareAbility()==1 ?"可以自理":"不能自理");
            result.add(ev);
        }

        return Result.success(result);
    }

    /**
     * 护工 与 老人绑定
     */
    @Override
    public Result binding(Long userId, Long elderlyId) {
        LambdaQueryWrapper<EmployeeElder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmployeeElder::getEmployeeId,userId);
        queryWrapper.eq(EmployeeElder::getElderlyId,elderlyId);

        EmployeeElder one = getOne(queryWrapper);
        if (one!=null){
            return Result.error("重复绑定");
        }
        EmployeeElder item = EmployeeElder.builder().employeeId(userId).elderlyId(elderlyId).build();
        save(item);
        return Result.success("绑定成功");

    }

}
